// 创建product类
class Sandwich {
    constructor() {
        this.ingredients = []
    }

    addIngredient (ingredient) {
        this.ingredients.push(ingredient)
    }

    toString () {
        return this.ingredients.join(', ')
    }
}

// 创建一个建造者类
class SandwichBuilder {
    constructor() {
        this.sandwich = new Sandwich()
    }

    reset () {
        this.sandwich = new Sandwich()
    }

    putMeat (meat) {
        this.sandwich.addIngredient(meat)
    }

    putCheese (cheese) {
        this.sandwich.addIngredient(cheese)
    }
    putVegetables (vegetables) {
        this.sandwich.addIngredient(vegetables)
    }
    get result () {
        return this.sandwich
    }
}

// 创建用户使用的builder
class SandwichMaker {
    constructor() {
        this.builder = new SandwichBuilder()
    }
    createCheeseSteakSandwich () {
        this.builder.reset()
        this.builder.putMeat('steak')
        this.builder.putCheese('cheese')
        this.builder.putVegetables(['peppers', 'onions'])
        return this.builder.result
    }
}

// 建造一个三明治
const sandwichMaker = new SandwichMaker()
const sandwich = sandwichMaker.createCheeseSteakSandwich()
console.log(`你的三明治是：${sandwich}`)